//
//  MyAddTribeListViewController.m
//  yigou
//
//  Created by wangdianlei on 2017/1/23.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "MyAddTribeListViewController.h"
#import "CommendTribeCell.h"
#import "MMMyAddTribe.h"
#import "TribeDetailViewController.h"
@interface MyAddTribeListViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *myaddArray;

@end

@implementation MyAddTribeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIView *blankView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    self.tableView.tableFooterView = blankView;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [ViewFactory setNavigationBlack:self];
    if (self.type == IMyAddTribeListShow) {
        self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"我加入的部落"];
    } else {
        self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"选择部落"];
    }
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(goPop)];
    self.navigationItem.leftBarButtonItem = backBtn;
    [self loadNetworkData];
}

- (void)goPop {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.myaddArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommendTribeCell *cell = GET_CELL(CommendTribeCell, @"CommendTribeCell");
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    MMMyAddTribe *one = self.myaddArray[indexPath.row];
    [cell fillWithData:one];
    
    if (self.type == IMyAddTribeListShow) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    } else if  (self.type == IMyAddTribeListChoose) {
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MMMyAddTribe *one = self.myaddArray[indexPath.row];
    if (self.type == IMyAddTribeListShow) {
        NSString *tribeID = one.tribe_id;
        TribeDetailViewController *detail = [[TribeDetailViewController alloc]initWithNibName:@"TribeDetailViewController" bundle:nil];
        [self.navigationController pushViewController:detail animated:YES];
        detail.tribeID = tribeID;
    } else if (self.type == IMyAddTribeListChoose) {
        [PublishData sharedPublishData].chooseTribeName = one.tribe_name;
        [PublishData sharedPublishData].chooseTribeID = one.tribe_id;
        [[NSNotificationCenter defaultCenter] postNotificationName:MSGSELECTTRIBE object:nil];
        [self goPop];
    }
}

- (void)loadNetworkData {
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *url = [NSString stringWithFormat:@"%@/hytribe/getMyJoinAllHyTribe", APIYIGOU];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dict = [NSDictionary changeType:request.responseJSONObject];
        [JSONModel addDatasFromDict:dict withModel:@"MMMyAddTribe" forKey:@"data" target:self.myaddArray isAppend:NO];
        [self.tableView reloadData];
    } error:^(YTKBaseRequest *request) {
        DYLog(@"");
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
}

ARRAY_INIT(myaddArray)

@end
